/* GUI CONVERTED from arpamte.p (converter v1.76) Tue Dec  3 02:59:27 2002 */
/* arpamte.p - AR PAYMENT MAINTENANCE Auto Apply Selection Criteria     */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.20.1.15 $                                               */
/*V8:ConvertMode=Maintenance                                            */
/* REVISION: 7.4      CREATED      : 08/06/93   BY: wep *H105*          */
/*                    LAST MODIFIED: 09/22/93   BY: bcm *H133*          */
/*                    LAST MODIFIED: 08/04/94   BY: bcm *H467*          */
/* REVISION: 7.4      LAST MODIFIED: 09/11/94   by: slm *H512*          */
/*                                   11/04/94   by: srk *FS79*          */
/*                                   12/01/94   BY: bcm *H577*          */
/*                                   12/15/94   by: pmf *F09V*          */
/*                                   12/28/94   by: str *H09L*          */
/*                                   06/22/95   by: wjk *H0F1*          */
/*                                   01/02/96   by: mys *H0HN*          */
/* REVISION: 8.5      LAST MODIFIED: 01/13/96   by: ccc *J053*          */
/*                                   07/21/96   by: *J0VY* M. Deleeuw   */
/* REVISION: 8.5      LAST MODIFIED: 07/29/96   by: taf *J101*          */
/* REVISION: 8.6      LAST MODIFIED: 01/23/97   by: bjl *K01G*          */
/*                                   04/09/97   by: bkm *J1J5*          */
/* REVISION: 8.6      LAST MODIFIED: 01/23/97   by: *K1DG* B. Gates     */
/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane    */
/* REVISION: 8.6E     LAST MODIFIED: 04/18/98   by: RUP      *L00K*     */
/* REVISION: 8.6E     LAST MODIFIED: 05/09/98   BY: *L00Y* Jeff Wootton */
/* Old ECO marker removed, but no ECO header exists *H027*                   */
/* REVISION: 8.6E     LAST MODIFIED: 06/02/98   BY: *L01K* Jaydeep Parikh    */
/* REVISION: 8.6E     LAST MODIFIED: 08/31/98   BY: *K1SX* Prashanth Narayan */
/* REVISION: 9.0      LAST MODIFIED: 03/10/99   BY: *M0B3* Michael Amaladhas */
/* REVISION: 9.0      LAST MODIFIED: 03/13/99   BY: *M0BD* Alfred Tan        */
/* REVISION: 9.1      LAST MODIFIED: 10/01/99   BY: *N014* Paul Johnson      */
/* REVISION: 9.1      LAST MODIFIED: 01/12/00   BY: *L0PH* Vihang Talwalkar  */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane  */
/* REVISION: 9.1      LAST MODIFIED: 08/11/00   BY: *N0KK* jyn               */
/* REVISION: 8.6E     LAST MODIFIED: 01/13/01   BY: *L17C* Jean Miller       */
/* REVISION: 9.1      LAST MODIFIED: 08/02/00   BY: *N0WP* Mudit Mehta       */
/* Old ECO marker removed, but no ECO header exists *F0PN*                   */
/* Revision: 1.20.1.9       BY: Katie Hilbert  DATE: 04/01/01 ECO: *P002*     */
/* Revision: 1.20.1.10      BY: Alok Thacker   DATE: 06/12/01 ECO: *M18Y*     */
/* Revision: 1.20.1.11      BY: Ed van de Gevel DATE: 04/16/02 ECO: *N1GP*    */
/* Revision: 1.20.1.12      BY: Hareesh V.      DATE: 06/13/02 ECO: *M1Z9*    */
/* Revision: 1.20.1.13    BY: Hareesh V.      DATE: 09/12/02 ECO: *M209*    */
/* $Revision: 1.20.1.15 $  BY: Ed van de Gevel DATE: 12/03/02  ECO: *N1NJ* */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/
/*!
  arpamte.p - AR Payment Auto Apply Document Selection Criteria
*/

/*!
   Controls the Range selection criteria for selecting AR Documents for payment.

   Input parameters:
      ar_recno
      ar_total
      jrnl

   Output parameters:
      cpl_code

*/

/* DISPLAY TITLE */
{mfdeclre.i}
{cxcustom.i "ARPAMTE.P"}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE arpamte_p_1 "Disc Amount"
/* MaxLen: Comment: */

&SCOPED-DEFINE arpamte_p_2 "Check Control"
/* MaxLen: Comment: */

&SCOPED-DEFINE arpamte_p_3 "Applied Amount"
/* MaxLen: Comment: */

&SCOPED-DEFINE arpamte_p_4 "Balance"
/* MaxLen: Comment: */

&SCOPED-DEFINE arpamte_p_6 "Sel"
/* MaxLen: Comment: */

&SCOPED-DEFINE arpamte_p_7 "Total"
/* MaxLen: Comment: */

&SCOPED-DEFINE arpamte_p_8 "Unapplied"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

{gldydef.i}
{gldynrm.i}
define input         parameter ar_recno as   recid.
define input         parameter artotal  like ar_amt.
{&ARPAMTE-P-TAG8}
define input         parameter jrnl     like glt_ref.
{&ARPAMTE-P-TAG9}
define output        parameter cpl_code like mfc_logical initial false.

{&ARPAMTE-P-TAG5}
define variable ard_recno    as recid.
define variable arwork_recno as recid.
define variable base_amt     like ar_amt.
define variable undo_all     like mfc_logical.
{&ARPAMTE-P-TAG6}

define shared variable rndmthd    like rnd_rnd_mthd.
define shared variable ar_amt_fmt as character.
define shared variable apply2_rndmthd like rnd_rnd_mthd.
define shared variable old_doccurr  like ar_curr.
{&ARPAMTE-P-TAG10}

define variable arselected    as logical initial false.
define variable first_sw_call as logical initial true.
define variable unappamt      like ar_amt.
define variable disc_draft    like mfc_logical initial false.
define variable memo_or_inv   like mfc_logical initial false.
/*VARS. inv_to_base_rate, rate2 REPLACES ar__dec01 INTRODUCED IN ETK*/
define variable inv_to_base_rate like ar_ex_rate no-undo.
define variable inv_to_base_rate2 like ar_ex_rate2 no-undo.
define variable mc-error-number like msg_nbr no-undo.

/*hk-s-024*/ define shared variable hk_amt  like ar_amt     no-undo.

/* MUST INCLUDE gprunpdf.i IN THE MAIN BLOCK OF THE PROGRAM SO THAT */
/* CALLS TO gprunp.i FROM ANY OF THE INTERNAL PROCEDURES SKIPS  */
/* DEFINITION OF SHARED VARS OF gprunpdf.i */
/* FOR FURTHER INFO REFER TO HEADER COMMENTS IN gprunp.i */
{gprunpdf.i "mcpl" "p"}
{&ARPAMTE-P-TAG13}

define buffer payment_ar_mstr for ar_mstr.
define buffer invcmemo_ar_mstr for ar_mstr.
define shared variable h-arpamtpl as handle no-undo.

/* REPLACED WORKFILE WITH TEMP-TABLE TO IMPROVE PERFORMANCE  */

define shared temp-table arautosel
   field sel_idr     as character format "X(1)"
   field ar_nbr      like ar_nbr
   field ar_check    like ar_check
   field ar_bill     like ar_bill
   field ar_type     like ar_type
   field ar_due_date like ar_due_date
   field ar_curr     like ar_curr
   field balance     like ar_amt
   field applied     like ar_amt
   field discount    like ar_amt
   field maxdisc     like ar_amt
   index ar-date ar_due_date.

/* DEFINE FORM TO DISPLAY CONTROL TOTALS */
FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
ar_mstr.ar_check       colon 10
   artotal                         label {&arpamte_p_2}
   ar_mstr.ar_amt         colon 58 label {&arpamte_p_7}
   ar_mstr.ar_bill        colon 10
   cm_sort                         no-label
   unappamt               colon 58 label {&arpamte_p_8}
 SKIP(.4)  /*GUI*/
with frame b1 side-labels width 80 NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-b1-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame b1 = F-b1-title.
 RECT-FRAME-LABEL:HIDDEN in frame b1 = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame b1 =
  FRAME b1:HEIGHT-PIXELS - RECT-FRAME:Y in frame b1 - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME b1 = FRAME b1:WIDTH-CHARS - .5.  /*GUI*/


/* SET EXTERNAL LABELS */
setFrameLabels(frame b1:handle).

/* DISPLAY FORMAT WAS CHANGED TO USE CDR FORMATTING */
/* DEFINE SCROLLING WINDOW FRAME TO TOGGLE SELECTED DOCUMENTS */
{&ARPAMTE-P-TAG1}
FORM /*GUI*/ 
   arautosel.sel_idr       /*format "X(1)"*/ label {&arpamte_p_6}
   arautosel.ar_nbr        format "X(8)"
   arautosel.ar_type
   arautosel.ar_due_date
   arautosel.balance       label  {&arpamte_p_4}
   arautosel.applied       label  {&arpamte_p_3}
   arautosel.discount      label  {&arpamte_p_1}
with frame c width 80
   title color normal
   (getFrameTitle("AUTOMATIC_PAYMENT_SELECTION",38)) THREE-D /*GUI*/.

   {&ARPAMTE-P-TAG2}

/* SET EXTERNAL LABELS */
setFrameLabels(frame c:handle).

/*PERFORM INITIAL READS*/
find ar_mstr where recid(ar_mstr) = ar_recno.
find cm_mstr where cm_addr = ar_mstr.ar_bill no-lock no-error.
find first gl_ctrl  no-lock.
find first arc_ctrl no-lock.

/* SET FORMATS ACCORDING TO PAYMENT CURRENCY ROUND METHOD */
assign
   ar_mstr.ar_amt:format in frame b1 = ar_amt_fmt
   artotal:format in frame b1 = ar_amt_fmt
   unappamt:format in frame b1 = ar_amt_fmt
   arautosel.balance:format in frame c   = ar_amt_fmt
   arautosel.applied:format in frame c   = ar_amt_fmt
   arautosel.discount:format in frame c  = ar_amt_fmt.

sw_block:
do on endkey undo, leave:
   if not batchrun then do:

      /*DISPLAY CONTROL TOTALS FRAME*/

      unappamt = artotal - ar_mstr.ar_amt.
      display ar_mstr.ar_check artotal ar_mstr.ar_amt ar_mstr.ar_bill
         unappamt
         cm_sort when (available cm_mstr)
      with frame b1.

      view frame c.

      /* INCLUDE SCROLLING WINDOW TO ALLOW THE USER TO SCROLL      */
      /* THROUGH (AND SELECT FROM) EXISTING PAYMENTS APPLICATIONS  */
      /* J053 ADDED THE '&INCLUDE3' INSTRUCTIONS TO FORCE CURRENCY */
      /* DEPENDENT FORMATTING OF THE AMOUNT VARIABLES.  HOWEVER,   */
      /* THE FORMATTING DOES NOT TAKE EFFECT UNTIL THE CURSOR BAR  */
      /* IS **AT** THE LINE ITEM CONTAINING THE VARIABLES.         */
      {&ARPAMTE-P-TAG3}
      {swselect.i
         &detfile=arautosel
         &scroll-field=arautosel.ar_nbr
         &framename="c"
         &framesize=12
         &sel_on=""*""
         &sel_off=""""
         &display1=arautosel.sel_idr
         &display2=arautosel.ar_nbr
         &display3=arautosel.ar_type
         &display4=arautosel.ar_due_date
         &display5=arautosel.balance
         &display6=arautosel.applied
         &display7=arautosel.discount
         &exitlabel=sw_block
         &exit-flag=first_sw_call
         &record-id=arwork_recno
         &include1="{arseloff.i}"
         &include2="{arselon.i}"
         &include3=" arautosel.balance:format in frame c = ar_amt_fmt.
                     arautosel.applied:format in frame c = ar_amt_fmt.
                     arautosel.discount:format in frame c = ar_amt_fmt. "
      }
      {&ARPAMTE-P-TAG4}
   end.

   /*CONVERT ar_amt (FOREIGN) TO BASE AND STORE IN ar_base_amt */
   {gprunp.i "mcpl" "p" "mc-curr-conv"
      "(input ar_mstr.ar_curr,
        input base_curr,
        input ar_mstr.ar_ex_rate,
        input ar_mstr.ar_ex_rate2,
        input ar_mstr.ar_amt,
        input true, /* ROUND */
        output ar_mstr.ar_base_amt,
        output mc-error-number)"}
   if mc-error-number <> 0 then do:
      {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}.
   end.


   /*CONVERT ar_applied (FOREIGN) TO BASE AND STORE IN ar_base_applied */
   {gprunp.i "mcpl" "p" "mc-curr-conv"
      "(input ar_mstr.ar_curr,
        input base_curr,
        input ar_mstr.ar_ex_rate,
        input ar_mstr.ar_ex_rate2,
        input ar_mstr.ar_applied,
        input true, /* ROUND */
        output ar_mstr.ar_base_applied,
        output mc-error-number)"}
   if mc-error-number <> 0 then do:
      {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=2}.
   end.

   /* TRAP ENDKEY */
   if keyfunction(lastkey) = "end-error"
      or lastkey = keycode("F4")
      or lastkey = keycode("CTRL-E") then do:
      hide frame b1 no-pause.
      undo sw_block, return.
   end.

   /*BEGIN PROCESSING FOR SELECTED RECORDS*/
   find payment_ar_mstr where recid(payment_ar_mstr) = recid(ar_mstr)
      no-lock.

   /* GET INVOICE OR MEMO TO BASE EXCHANGE RATE FROM qad_wkfl */
   {argetwfl.i
      payment_ar_mstr.ar_nbr
      inv_to_base_rate
      inv_to_base_rate2}

   det_loop:
   for each arautosel where sel_idr = "*",
         first invcmemo_ar_mstr
         where invcmemo_ar_mstr.ar_nbr = arautosel.ar_nbr:
      /*TURN ON FLAG THAT SAYS WE SELECTED SOMETHING*/
      arselected = true.
      /*GET THE INVOICE OR MEMO'S ROUND METHOD*/
      run get_invcmemo_rounding_method in h-arpamtpl
         (input invcmemo_ar_mstr.ar_curr, output apply2_rndmthd).
      if apply2_rndmthd = ? then next.
      /*RESET THE EXCHANGE RATES AND CURRENCY OF THE PAYMENT IF*/
      /*THE INVOICE OR MEMO IS A DISCOUNTED DRAFT.  PER DRAFT*/
      /*DISCOUNTING PROGRAM 'DMDISC.P', THE CURRENCY STORED IN*/
      /*AR_SLSPSN[1] MAY **ONLY*** BE AR_CURR OR BASE*/
      if invcmemo_ar_mstr.ar_type = "D"
         and ( invcmemo_ar_mstr.ar_dd_ex_rate <> 1 or
         invcmemo_ar_mstr.ar_dd_ex_rate2 <> 1 or
         invcmemo_ar_mstr.ar_dd_curr <> "" )  then do:
         /*USE THE NEW VARS DEFINED FOR DRAFT DISCOUNTING CURRENCY */

         if invcmemo_ar_mstr.ar_dd_curr   = base_curr
            then rndmthd = gl_rnd_mthd.
         else
            if invcmemo_ar_mstr.ar_dd_curr   <> payment_ar_mstr.ar_curr
            then next det_loop.
         disc_draft = true.
         /*USE THE NEW VARS DEFINED FOR DRAFT DISC CURR & RATE */

         assign
            payment_ar_mstr.ar_ex_rate = invcmemo_ar_mstr.ar_dd_ex_rate
            payment_ar_mstr.ar_ex_rate2 = invcmemo_ar_mstr.ar_dd_ex_rate2
            payment_ar_mstr.ar_curr = invcmemo_ar_mstr.ar_dd_curr.
      end.
      else
         memo_or_inv = true.
      /*UPDATE THE INVOICE OR MEMO WITH PAYMENT DETAIL*/

      /* PASSING BUFFERS INVCMEMO_AR_MSTR AND PAYMENT_AR_MSTR AS */
      /* PARAMETERS TO INTERNAL PROCEDURES IN ARPAMTPL.P AND     */
      /* REMOVED DEFINING BUFFERS IN THESE INTERNAL PROCEDURES   */

      run create_payment_detail in h-arpamtpl
         (input payment_ar_mstr.ar_nbr,
         input invcmemo_ar_mstr.ar_nbr,
         input invcmemo_ar_mstr.ar_acct,
         input invcmemo_ar_mstr.ar_sub,
         input invcmemo_ar_mstr.ar_cc,
         input invcmemo_ar_mstr.ar_entity,
         input invcmemo_ar_mstr.ar_type,
         input arautosel.applied,
         input arautosel.discount,
         input dft-daybook,
         input nrm-seq-num,
         output ard_recno,
         buffer invcmemo_ar_mstr,
         buffer payment_ar_mstr).

      /*CALCULATE TAX DETAIL*/
      run calculate_tax_adjustment in h-arpamtpl
         (input  ard_recno,
         buffer invcmemo_ar_mstr,
         buffer payment_ar_mstr).

      {&ARPAMTE-P-TAG11}
      {&ARPAMTE-P-TAG12}

      /*POST GL TRANSACTIONS*/
      /* ADDING 15TH PARAMETER - LOGICAL INPUT PARAMETER TO INDIACTE */
      /* CALLING FUNCTION                                            */
/*hk-s-024*/  hk_amt = arautosel.applied.      
      run post_gl_transactions in h-arpamtpl
         (input recid(payment_ar_mstr),
         input ard_recno,
         input payment_ar_mstr.ar_curr,
         input inv_to_base_rate,
         input inv_to_base_rate2,
         input payment_ar_mstr.ar_ex_rate,
         input payment_ar_mstr.ar_ex_rate2,
         input arautosel.applied,
         input arautosel.discount,
         input recid(invcmemo_ar_mstr),
         input invcmemo_ar_mstr.ar_ex_rate,
         input invcmemo_ar_mstr.ar_ex_rate2,
         input apply2_rndmthd,
         input jrnl,
         input true,
         output undo_all).
      if undo_all then undo sw_block, leave.
      {&ARPAMTE-P-TAG14}
      /*UPDATE CUSTOMER BALANCE*/
      run update_customer_balance in h-arpamtpl
         (buffer invcmemo_ar_mstr,
          input ard_recno).
      {&ARPAMTE-P-TAG7}
   end.  /*det_loop*/

   /*CHECK TO SEE IF ANY RECORDS WERE PROCESSED*/
   if not arselected then do:
      /* NO INV/MEMOS SELECTED FOR PROCESSING */
      {pxmsg.i &MSGNUM=1179 &ERRORLEVEL=2}
      do on endkey undo sw_block, retry sw_block:
         pause.
      end.
   end.
   else if disc_draft and memo_or_inv then do:
      /* DISCOUNTED DRAFT AND NORMAL MEMO */
      /* INCLUDED IN SELECTION            */
      {pxmsg.i &MSGNUM=3556 &ERRORLEVEL=2}
      pause.
   end.

   leave sw_block.
end.  /* SW_BLOCK */

hide frame c no-pause.
hide frame b1 no-pause.
cpl_code = true.
